<?php
require_once('../../../config.php');
require_once($CFG->libdir . '/formslib.php');
require_once($CFG->libdir . '/adminlib.php');
require_once($CFG->dirroot . '/local/blackboxsurvey/lib.php');
require_once($CFG->dirroot . "/local/blackboxsurvey/forms.php");
define('AJAX_SCRIPT', true);

//yeu cau login,sau khi login tu dong chuyen sang trang chu
global $USER;
if ($USER->id == null){
    $SESSION->homepage = true;
    require_login($SITE);
}
if($SESSION->homepage){
    $SESSION->homepage = null;
    redirect( $CFG->wwwroot);
}
//class
require_once($CFG->dirroot . "/local/blackboxsurvey/class/survey.php");
require_once($CFG->dirroot . "/local/blackboxsurvey/class/questionaire.php");
require_once($CFG->dirroot . "/local/blackboxsurvey/class/questions.php");
require_once($CFG->dirroot . "/local/blackboxsurvey/class/question_choice.php");

//path
$path = $CFG->wwwroot.'/local/blackboxsurvey/lecturer/manage_questionaire.php';
$path_image = $CFG->wwwroot .'/local/blackboxsurvey/image/';
$array['path_image'] = $path_image;
try{
$id = required_param('idbbsurvey', PARAM_TEXT);
$id_subject = required_param('idsubject', PARAM_TEXT);

//tao id cho questionaire
global $USER;
$c_qn = new b_questionaire();
$qn_id = $c_qn->create_questionaire_id($id,$USER->id,$id_subject);
$array['questionaire_id'] = $qn_id;
//kiem tra so request moi
$new_request = $c_qn->count_new_request($id, $id_subject, $USER->id);
if($new_request > 0)
    $array['new_request'] = '('.$new_request.' new requests)';
else
    $array['new_request'] = '';
//xoa cac du lieu luu tam truoc do
$c_qn->delete_temp_questionaire_data($qn_id);
//cho nay goi cai dong ham lay chi tiet phieu khao sat
$c_survey = new b_survey();
$b_ques = new b_questions();
$b_ques_choice = new b_question_choice();
$survey = $c_qn->get_questionaire_info($id,$id_subject);//lay thong tin co ban cua phieu khao sat

//gan nhung du lieu vao $arry de hien tren form
$array['program'] = $survey->program_name;
$array['id_bb'] = $id;
$aaray['id_sub'] = $id_subject;
$array['subject'] = $survey->subject_name_vn;
$array['start'] = $survey->date_start;
$array['end'] = $survey->date_end;
$array['id_program'] = $survey->p_id;

//lay cau hoi cua phieu khao sat
$questions = $c_survey->get_survey_question_bySurveyID($id);
$array['ques_num'] = count($questions);
$count = 1;

//lay cau hoi da add vao survey
foreach($questions as $q) {
    $q_content = preg_replace(array('#<p>#', '#</p>#'), '', $q->content, 1);
    $array['ques_id'.$count] = $q->id;
    $array['ques_content'.$count] = $count.'- '.$q_content;
    //goi ham lay tat ca choice cua cau hoi
    $ques_choice = $b_ques_choice->get_choice_by_question($q->ques_id);
    //dem so luong option
    $array['num_choice'.$count] = count($ques_choice);
    $n = 1;
    //chay foreach de gan choice vao array
    $array['type'.$count] = $q->id_type;
    foreach($ques_choice as $c){
        $array['choice_id'.$count.$n] = $c->id;
        $array['choice_content'.$count.$n] = $c->content;
        $n++;
    }
    $count++;  
}
//Lay guide in
$array['guide_in'] = $survey->guide_input;
//Lay guide out
$array['guide_out'] = $survey->guide_output;
//Lay print
$array['print_line'] = $survey->print_line;
}catch(Exception $ex){}


//Them thanh menu ngang ung voi trang trong duong link
navigation_node::override_active_url(new moodle_url($path, array('param'=>'value')));

/* Chuan bi trang web can hien thi */	


$url = new moodle_url($path);
$PAGE->set_url($url);
$PAGE->set_title('Questionnaire Form');
$PAGE->set_heading('Questionnaire Form');
$PAGE->set_pagelayout('standard');

echo $OUTPUT->header();

$mform = new form_do_questionaire(null, $array);
/* Viet doan code cua ban o day */
if ($fromform = $mform->get_data()) 
{	
    
    $c_qn = new b_questionaire();
    $id_qn = $fromform->qn_id;
    
    //save lai question answer
    $ques_id = $_POST['ques_id'];
    $ques_type = $_POST['ques_type'];
    $n = count($ques_id);
    $str = '';
    for($i = 0; $i < $n; $i++)
    {
        $x = 'ques_ans'.($i+1);
        $ques_ans = $_POST[$x];
        $c_qn->save_ques_ans_questionaire($id_qn, $ques_id[$i], $ques_ans, $ques_type[$i]);
    }
    //save danh sach mon cung cap
    $insub = $_POST['insub_cb'];
    $c_qn->save_input_subject_list($id_qn,$insub);
    
    //save input data
    $input = $_POST['input_cb'];
    $ni = count($input);
    for($i = 0; $i < $ni; $i++)
    {
        $c_qn->update_input_data_status($input[$i]);
    }
    
    //save danh sach mon output
    $outsub = $_POST['outsub_cb'];
    $c_qn->save_output_subject_list($id_qn,$outsub);
    
    //save input data
    $output = $_POST['output_cb'];
    $n = count($output);
    for($i = 0; $i < $n; $i++)
    {
        $c_qn->update_output_data_status($output[$i]);
    }
    
    //save course map data cua 
    $c_qn->save_course_map_byAccepted_request($id_qn);
    
    //update status phieu khao sat
    $action = $fromform->txt_action;
    
    $date = date('Y-m-d H:i:s');
    $submit_date = strtotime($date);
    $c_qn->update_questionaire_status($id_qn, $action, $submit_date);
    
    //tao thong bao new request.
    if($action == 1)
    {
        for($i = 0; $i < $ni; $i++)
        {
            $c_qn->create_request_mess($input[$i]);
        }
    }
    
    //chuyen den trang manage_questionaire.php
    echo '<script>location.href="'.$path.'"</script>';
    
    
    //echo "<script  type='text/javascript'>alert('".$fromform->txt_action."');</script>";
}


$mform->display();
/*------Ket thuc doan code cua ban-------*/

echo $OUTPUT->footer();